#include "app_buzzer.h"
#include <Arduino.h>

#include "board_def.h"
#include "SoftwareTimer.h"


int Buzzer_Freq = 1000;
int Buzzer_PWM_Channel = 0;
int Buzzer_PWM_Duty = 512;
int Buzzer_PWM_Res = 10;


 
SoftwareTimer buzzerTimer;

void app_buzzer_init(void)
{   
    pinMode(PIN_BUZZER, OUTPUT);
    digitalWrite(PIN_BUZZER, LOW);

    ledcSetup(Buzzer_PWM_Channel, Buzzer_Freq, Buzzer_PWM_Res);
    ledcAttachPin(PIN_BUZZER, Buzzer_PWM_Channel);

    app_buzzer_off();
}


void app_buzzer_on(void)
{
    ledcWriteTone(Buzzer_PWM_Channel, Buzzer_Freq);
    ledcWrite(Buzzer_PWM_Channel, Buzzer_PWM_Duty);
}

void app_buzzer_off(void)
{
    ledcWrite(Buzzer_PWM_Channel, 0);
}

static void buzzer_timer_callback(TimerHandle_t xTimerID)
{
    // freeRTOS timer ID, ignored if not used
    (void)xTimerID;
    app_buzzer_off();
    buzzerTimer.~SoftwareTimer();
}
TimerHandle_t buzzer_timerhandle;
void app_buzzer_once(uint32_t ms) //ms
{

    buzzerTimer.begin(ms, buzzer_timer_callback, NULL, false);
    // // Start the timer
    if (!buzzerTimer.start())
    {
        // tft.println("timer start fail");
        return;
    }
    app_buzzer_on();
}
